package com.jfcloud.mall.order.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jfcloud.common.annotation.CheckToken;
import com.jfcloud.common.aspect.TokenUser;
import com.jfcloud.common.http.RespDataT;
import com.jfcloud.mall.order.domain.dto.OrderSubmitForm;
import com.jfcloud.mall.order.domain.dto.OrderVo;
import com.jfcloud.mall.order.service.OrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "订单相关API")
@Controller
@RequestMapping("/order")
public class OrderController {
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
	
	@Autowired
	private OrderService orderService;
	
	@ApiOperation("创建订单")
	@PostMapping(path = "/create")
	@ResponseBody
	@CheckToken
	public RespDataT<OrderVo> create(@RequestBody OrderSubmitForm orderInfo, HttpServletRequest request, HttpServletResponse response) {
		try {
			TokenUser tokenUser=TokenUser.load(request);
			OrderVo token = orderService.createOrder(orderInfo,tokenUser,orderInfo.getToken());
			return new RespDataT<OrderVo>(token);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT<OrderVo>(e);
		}
	}
}
